import com.alibaba.fastjson.JSONObject;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class HBaseHomeWork {
    static String name = "user_relate";

    public static void create_table(HBaseAdmin baseAdmin) throws IOException {

        final HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(name));
        descriptor.addFamily(new HColumnDescriptor("friends"));
        baseAdmin.createTable(descriptor);

    }

    public static void update(Connection conn) throws IOException {
        final HTable user_relate = (HTable) conn.getTable(TableName.valueOf(name));
        final Put uid1 = new Put(Bytes.toBytes("uid1"));
        final Put uid2 = new Put(Bytes.toBytes("uid2"));
        final Put uid3 = new Put(Bytes.toBytes("uid3"));
        final Put uid4 = new Put(Bytes.toBytes("uid4"));
        final Put uid5 = new Put(Bytes.toBytes("uid5"));
        final Put uid6 = new Put(Bytes.toBytes("uid6"));

        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid2"), Bytes.toBytes("uid2"));
        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid3"), Bytes.toBytes("uid3"));
        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid4"), Bytes.toBytes("uid4"));
        uid1.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid5"), Bytes.toBytes("uid5"));
        uid2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid1"), Bytes.toBytes("uid1"));
        uid2.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid3"), Bytes.toBytes("uid3"));
        uid3.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid1"), Bytes.toBytes("uid1"));
        uid3.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid2"), Bytes.toBytes("uid2"));
        uid4.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid1"), Bytes.toBytes("uid1"));
        uid5.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid1"), Bytes.toBytes("uid1"));
        uid6.addColumn(Bytes.toBytes("friends"), Bytes.toBytes("uid1"), Bytes.toBytes("uid1"));

        user_relate.put(uid1);
        user_relate.put(uid2);
        user_relate.put(uid3);
        user_relate.put(uid4);
        user_relate.put(uid5);
        user_relate.put(uid6);
        if (null != user_relate) user_relate.close();
    }

    public static void main(String[] args) throws IOException {
        final Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "linux121,linux122");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        final Connection conn = ConnectionFactory.createConnection(conf);
        final HBaseAdmin baseAdmin = (HBaseAdmin) conn.getAdmin();

        //create_table(baseAdmin);
        update(conn);

        if (null != baseAdmin) baseAdmin.close();
        if (null != conn) conn.close();
    }
}
